package ru.agiledev.akb.server.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;

/**
 * Created by a.kirillov
 * Date: 5/17/11 Time: 3:22 PM
 */
public class Translate {

	public static String toTranslit(String s_Rus) {
		int i1, ln_pos;
		String tr = "", ss = " ";
		String rusb = "АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ";
		String latb = "ABVGDEEGZIYKLMNOPRSTUFXCCSS`Y`EUY";
		String rsogl = "БВГДЖЗКЛМНПРСТФХЦЧШЩ";
		int lent = s_Rus.length();
		for(int il = 0; il < lent; il++) {
			String s1 = s_Rus.substring(il, il + 1);
			String s2 = s1;
			if (s1.equals("Е")) {
				ln_pos = rsogl.indexOf(ss);
				if (il == 1)
					s1 = "E";
				else
					if (ln_pos > 0)
						s1 = "E";
					else
						s1 = "YE";
			} else
				if (s1.equals("Ё")) {
					if (il == 1)
						s1 = "YO";
					else
						if ("ЖЧШЩ".indexOf(ss) != -1)
							s1 = "E";
						else
							if (rsogl.indexOf(ss) != -1)
								s1 = "YE";
							else
								s1 = "YO";
				} else
					if (s1.equals("И")) {
						if (ss.equals("Ь"))
							s1 = "YI";
						else
							s1 = "I";
					} else
						if (s1.equals("Ж"))
							s1 = "ZH";
						else
							if (s1.equals("Х"))
								s1 = "KH";
							else
								if (s1.equals("Ц"))
									s1 = "TS";
								else
									if (s1.equals("Ч"))
										s1 = "CH";
									else
										if (s1.equals("Ш"))
											s1 = "SH";
										else
											if (s1.equals("Щ"))
												s1 = "SHCH";
											else
												if (s1.equals("Ю"))
													s1 = "YU";
												else
													if (s1.equals("Я"))
														s1 = "YA";
													else {
														i1 = rusb.indexOf(s1);
														if (i1 != -1)
															s1 = latb.substring(i1, i1 + 1);
													}
			tr = tr + s1;
			ss = s2;
		}
		return tr;
	}

	@SuppressWarnings("unchecked")
	public static String fromTranslit(String s_Eng) {
        if (s_Eng == null || s_Eng.isEmpty()) {
            return "";
        }

		HashMap<String,String> dict = new HashMap<String,String>();
		dict.put("SHCH", "Щ");
		dict.put("SCH", "Щ");
		dict.put("KH", "Х");
		dict.put("TS", "Ц");
		dict.put("CH", "Ч");
		dict.put("SH", "Ш");
		dict.put("YI", "ЬИ");
		dict.put("ZH", "Ж");
		dict.put("YE", "ЬЕ");
		dict.put("YO", "Ё");
		dict.put("YA", "Я");
		dict.put("YU", "Ю");
		dict.put("JO", "Ё");
		dict.put("JA", "Я");
		dict.put("JU", "Ю");
		dict.put("TS", "Ц");
		dict.put("IU", "Ю");
		dict.put("IA", "ИЯ");
		dict.put("EY", "ЕЙ");
		dict.put("IY", "ИЙ");
		dict.put("JOE", "ДЖОИ");
		dict.put("OY", "ОЙ");
		dict.put("OU", "У");
		dict.put("AY", "АЙ");
		dict.put("OYA", "ОЯ");
		dict.put("ZX", "З");
		dict.put("TS", "Ц");
		dict.put("Q", "КВ");
		dict.put("X", "КС");
		ArrayList<String> keys = new ArrayList<String>(dict.keySet());
		Collections.sort(keys, new Comparator() {
            public int compare(Object a, Object b) {
                if (a.toString().length() > b.toString().length())
                    return 0;
                return 1;
            }
        });
		String tr = "", ss = s_Eng.toUpperCase();
		String rusb = "АБВГДЕЁЖЗИЫКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯХВЯ ;АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ";
		String latb = "ABVGDEEGZIYKLMNOPRSTUFXCCSS`Y'EUYHWJ ;АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ";
		String pre_latb = "BCDFGHKLMNPRSTWZ";
		if (ss.length() > 1 && ss.charAt(0) == 'Y' && ss.charAt(1) == 'E') {
			tr += "Э";
			ss = ss.substring(2);
		}
		while(!ss.isEmpty()) {
			for(String cmp : keys)
				if (ss.startsWith(cmp)) {
					tr += dict.get(cmp);
					ss = ss.substring(cmp.length());
					continue;
				}
			if (ss.isEmpty())
				break;
			if (latb.indexOf(ss.charAt(0)) != -1)
				tr += rusb.charAt(latb.indexOf(ss.charAt(0)));
			if (ss.length() == 2 || (ss.length() > 2 && ss.charAt(2) == ' '))
				if (ss.charAt(1) == 'Y') {
					if (pre_latb.indexOf(ss.charAt(0)) != -1)
						tr += "ИЙ";
					else
						tr += "Й";
					break;
				}

			ss = ss.substring(1);
		}
		return tr;
	}

}

